package com.dgut.crm.system.entity.dto;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.BeanUtils;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.StringUtils;

import com.dgut.crm.common.util.enumType.Sex;
import com.dgut.crm.system.entity.Department;
import com.dgut.crm.system.entity.Number;
import com.fasterxml.jackson.annotation.JsonFormat;


public class NumberQueryDTO {
	
	private Long id;
	private String userName; //用户名
	private String password;
	private String realName;
	private String email; //邮箱
	private String phone;
	private Sex sex;
	@DateTimeFormat(pattern="yyyy/MM/dd HH:mm:ss") 
	private Date birthday;
	private String address;
	private String idcard;
	@DateTimeFormat(pattern="yyyy/MM/dd HH:mm:ss")  
	private Date workTime; //工作时间
	
	private Integer workYear;
	
	private Long departmentId;
	private String departmentName;
	
	public static void dtoToEntity(NumberQueryDTO dto , Number number) {
		BeanUtils.copyProperties(dto, number);
	}
	public static void entityToDto(NumberQueryDTO dto , Number number) {
		
		BeanUtils.copyProperties(number,dto);
		Department department  = number.getDepartment();
		if(department!=null) {
			dto.setDepartmentId(department.getId());//把关系传递到View
			dto.setDepartmentName(department.getName());
		}
	}
	
	public static Specification<Number> getSpecification(NumberQueryDTO numberQuryDTO)
	{
		Specification<Number> spec = new Specification<Number>() {
			public Predicate toPredicate(Root<Number> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				//1.Predicate查询条件集合
				 List<Predicate> list = new ArrayList<Predicate>(); 
				 
				 
				//2.根据 QueryDTO数据字段的值进行判断以及条件的组装
				 if(null != numberQuryDTO && !StringUtils.isEmpty(numberQuryDTO.getUserName())) {
					 Predicate  p1 =  cb.like(root.get("userName").as(String.class),"%"+ numberQuryDTO.getUserName() + "%");
					 list.add(p1);
			      }
				 
				 if(null != numberQuryDTO && !StringUtils.isEmpty(numberQuryDTO.getWorkYear())) {
					 Predicate  p2 =  cb.gt(root.get("workYear").as(Integer.class),numberQuryDTO.getWorkYear());
					 list.add(p2);
				 }
				 if(null != numberQuryDTO && !StringUtils.isEmpty(numberQuryDTO.getRealName())) {
					 Predicate  p3 =  cb.like(root.get("realName").as(String.class),"%"+ numberQuryDTO.getRealName() + "%");
					 list.add(p3);
				 }
				 
				 //3.Predicate查询条件集合的 size 创建对应的Predicate查询条件数组
				 Predicate[] p = new Predicate[list.size()];  
				 //4.CriteriaBuilder的and 函数组装 查询条件数组
				 return cb.and(list.toArray(p));  
			}
		};
		return spec;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	
	@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss",timezone = "GMT+8")
	public Date getWorkTime() {
		return workTime;
	}
	@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss",timezone = "GMT+8")
	public Date getBirthday() {
		return birthday;
	}
	public void setWorkTime(Date workTime) {
		this.workTime = workTime;
	}
	
	public Integer getWorkYear() {
		return workYear;
	}
	public void setWorkYear(Integer workYear) {
		this.workYear = workYear;
	}
	
	public String getRealName() {
		return realName;
	}
	public void setRealName(String realName) {
		this.realName = realName;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Long getDepartmentId() {
		return departmentId;
	}
	public void setDepartmentId(Long departmentId) {
		this.departmentId = departmentId;
	}
	public String getDepartmentName() {
		return departmentName;
	}
	public void setDepartmentName(String departmentName) {
		this.departmentName = departmentName;
	}
	
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public Sex getSex() {
		return sex;
	}
	public void setSex(Sex sex) {
		this.sex = sex;
	}
	
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getIdcard() {
		return idcard;
	}
	public void setIdcard(String idcard) {
		this.idcard = idcard;
	}
	@Override
	public String toString() {
		return "NumberQueryDTO [id=" + id + ", userName=" + userName + ", workTime=" + workTime + ", realName="
				+ realName + ", workYear=" + workYear + ", departmentId=" + departmentId + ", departmentName="
				+ departmentName + "]";
	}
}
